package genetic.q1.functions.sumlist.statements;

import genetic.q1.WithallPhenotype;
import genetic.q1.functions.Operator;
import genetic.q1.functions.Program;
import genetic.q1.functions.ProgramObject;
import genetic.q1.functions.Statement;
import genetic.q1.functions.Variable;

public class If extends Statement {

	boolean started;

	private If(ProgramObject[] evals) {
		super("If", evals);
		started = false;
	}

	public If(){
		super("If", null);
	}

	@Override
	public Statement CreateInstance(ProgramObject[] evals) {
		return new If(evals);
	}

	@Override
	public ProgramObject choose(int j, int i, WithallPhenotype withallPhenotype) {
		if (j==0) {
			return withallPhenotype.getFromReadWriteVars(i);
		} else if (j==1) {
			return withallPhenotype.getFromComparisons(i);
		} else if (j==2) {
			return withallPhenotype.getFromReadWriteVars(i);
		}
		return null;
	}

	@Override
	public int execute(Program program) {
		if (!started){
			started = true;
			int val1 = ((Variable)evals[0]).getValue();
			int val2 = ((Variable)evals[2]).getValue();
			Operator op = (Operator)(evals[1]);
			if (op.compare(val1, val2)){
				return program.getCounter() + 1;
			} else {
				return getParam();
			}
		} else {
			return getParam();
		}
	}

	@Override
	public String getObjectName() {
		return "if("+evals[0].getObjectName()+","+evals[1].getObjectName()+","+evals[2].getObjectName()+");";
	}

	@Override
	public boolean isBeginStatement() {
		return true;
	}

	@Override
	public int getNumVars() {
		return 3;
	}

	@Override
	public String toString() {
		return "if (" + evals[0].toString() + " " + evals[1].toString() + " " + evals[2].toString() + ") {  // " + getParam();
	}
	
	@Override
	public void reset() {
		started = false;
	}
}
